// pages/resumeDetail/resumeDetail.js
var app=getApp();
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    httpApiImg: app.globalData.httpApiImg,
    showSelect:false,
    detail: '' ,
    workInfo:[],
    educationInfo:[],
    projectInfo:[],
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    this.getDetail(options.id);
    this.getWorkInfo(options.userId);
    this.getEducationInfo(options.userId);
    this.getProjectInfo(options.userId);
    wx.showLoading();
  },
  getDetail: function (id) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/getRecruitPersonalInfo', //仅为示例，并非真实的接口地址
      data: { 
        id: id,
        userId:this.data.userInfo.id  
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data) {
            if (res.data.data.createTime) {
              res.data.data.time = util.formatDate(res.data.data.createTime);
            }
            if (res.data.data.enterWorkDate) {
              var year = new Date(res.data.data.enterWorkDate.replace(/\-/g, "/"));
              var now = new Date();
              var workYear = parseInt(now - year) / 1000 / 3600 / 24 / 365;
              res.data.data.workYear = workYear.toFixed(0);
            }
            that.setData({
              detail: res.data.data
            })
          }
        }
      }
    })
  },
  getWorkInfo:function(id){
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitWorkInfo/getByUserId', //仅为示例，并非真实的接口地址
      data: { userId: id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data) {
            that.setData({
              workInfo: res.data.data.pp
            })
          }
        }
      }
    })
  },
  getEducationInfo: function (id) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitEducationInfo/getByUserId', //仅为示例，并非真实的接口地址
      data: { userId: id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data) {
            that.setData({
              educationInfo: res.data.data.pp
            })
          }
        }
      }
    })
  },
  getProjectInfo: function (id) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitProjectInfo/getByUserId', //仅为示例，并非真实的接口地址
      data: { userId: id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data) {
            that.setData({
              projectInfo: res.data.data.pp
            })
          }
        }
      }
    })
  },
  collect: function (e) {//收藏/取消收藏
    var that = this;
    var url = 'tGyCollection/deleteCollectionRecordByParams';
    if (this.data.detail.isCollection == 0) {
      url = 'tGyCollection/save';
    }
    wx.request({
      url: app.globalData.httpApiCar + url, //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        sourceId: this.data.detail.id,
        userId: this.data.userInfo.id,
        block: 6
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            'detail.isCollection': that.data.detail.isCollection == 0 ? 1 : 0
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  toggleSelect: function (e) {//电话联系
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: this.data.detail.telephone //仅为示例，并非真实的电话号码
    })
    this.setData({
      showSelect: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})